<?php 
namespace Home\Controller;
class UserController extends HomeController
{
    public function index(){

// 手册上的分页做法
        $user_id = $_SESSION['user']['id'];
       /* $article = M('article'); // 实例化User对象
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $article->field('a.title,a.id,c.c_content,c.com_time,c.user_id,u.name')
                                ->table('hx_article a,hx_comment c,hx_user u')
                                ->where('a.user_id = '.$user_id.' AND c.art_id = a.id AND a.status = 1 AND c.status = 1 AND u.id = c.user_id')->page($_GET['p'].',5')->select();

        $this->assign('list',$list);// 赋值数据集
        $count = $article->table('hx_article a,hx_comment c,hx_user u')
                                ->where('a.user_id = '.$user_id.' AND c.art_id = a.id AND a.status = 1 AND c.status = 1 AND u.id = c.user_id')->count();// 查询满足要求的总记录数

        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('title', '个人中心-虎嗅网');
        $this->display('User/index');
*/
// 引用函数的分页做法
        $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
        $m=M('article');
        $count = $m->table('hx_article a,hx_comment c,hx_user u')
                                ->where('a.user_id = '.$user_id.' AND c.art_id = a.id AND a.status = 1 AND c.status = 1 AND u.id = c.user_id')->count();// 查询满足要求的总记录数
        $p=getpage($m,$count,5);
        $list=$m->field('a.title,a.id,c.c_content,c.com_time,c.user_id,u.name')
                                ->table('hx_article a,hx_comment c,hx_user u')
                                ->where('a.user_id = '.$user_id.' AND c.art_id = a.id AND a.status = 1 AND c.status = 1 AND u.id = c.user_id')->page($_GET['p'].',5')->order('c.id DESC')->select();
        $this->list=$list;
        $this->page=$p->show();
        $this->assign('title', '个人中心-虎嗅网');
        $this->display('User/index');
    }

    public function doAjax(){
    M('user')->create();

    $obj=M('user') -> where(array('name'=>$_SESSION['user']['name']))->save(array('self_intro'=>$_POST['self_intro'],'motto'=>$_POST['self_intro']));
    if ($obj) {
       echo true;
       $_SESSION['user']['self_intro'] = $_POST['self_intro'];
    } else {
        echo false;
    }

    }

    public function edit()
    {
        $this->display('User/edit');
    }

    public function account_info()
    {
        
        // V($_SESSION);
        // exit;

        // 判断附件是否有上传
        // 如果有则实例化Upload,把附件上传到服务器指定位置
        // 然后把附件的路径名或得到，存入$_post
       
            if (!empty($_FILES)) {
                $upload = new \Think\Upload();
                $upload->rootPath = './Uploads/'; // 设置附件上传根目录
                $upload->savePath = 'head_img/'; // 设置附件上传（子）目录
                $info = $upload -> uploadOne($_FILES['head_img']);
                if(!$info) {
                    $this->error($upload->getError());//获得上传附件产生的错误信息
                } else {
                   
                       $head_img =  $info['savepath'].$info['savename'];
                    
                        $name = array('name'=>$_SESSION['user']['name']);
                        $_POST['head_img'] = $head_img;

                        // 把已经上传好的图片制作缩略图
                        $image = new \Think\Image();
                        // open(); 打开图像资源，通过路径名找到图片
                        $srcimg = $upload->rootPath.$head_img;
                        $image -> open($srcimg);

                        // 缩放图片为200x200 并将路径存入POST
                        $image -> thumb(200,200);//按照比例缩小
                        $img_200 = $info['savepath']."200_".$info['savename'];
                        $image -> save($upload->rootPath.$img_200);
                        $_POST['img_200'] = $img_200;

                        // 缩放图片为150x150 并将路径存入POST
                        $image -> thumb(150,150);//按照比例缩小
                        $img_150 = $info['savepath']."150_".$info['savename'];
                        $image -> save($upload->rootPath.$img_150);
                        $_POST['img_150'] = $img_150;

                        // 缩放图片为40x40 并将路径存入POST
                        $image -> thumb(40,40);//按照比例缩小
                        $img_40 = $info['savepath']."40_".$info['savename'];
                        $image -> save($upload->rootPath.$img_40);
                        $_POST['img_40'] = $img_40;

            }

        M('headimg')->create();//post表单数据
        $row = M('headimg')->add();
        $headimg_id = array('headimg_id'=>$row);
        if($row >0) {
            M('user')->where($name)->save($headimg_id);
            // 取出头像信息
            $headimg = M('headimg');
            $headimg_info = $headimg->where(array('id'=>$row))->getField('img_200,img_150,img_40');
            $_SESSION['user']['headimg_id'] = $row;
            $data = array();
            foreach ($headimg_info as $val) {
                foreach ($val as $k => $v) {
                    $_SESSION['user'][$k] =$v;
                }
            }
            $this->success('上传成功!');
        } else {
            $this->error('上传失败!');

        }

        // $upload = new \Think\Upload();// 实例化上传类
        // $upload->maxSize = 3145728 ;// 设置附件上传大小
        // $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        // $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // $upload->savePath = ''; // 设置附件上传（子）目录
        // // 上传文件
        // $info = $upload->upload();
        // if(!$info) {// 上传错误提示错误信息
        // $this->error($upload->getError());
        // }else{// 上传成功 获取上传文件信息
        // foreach($info as $file){
        // echo $file['savepath'].$file['savename'];
        // }
        }

        $this->display('User/account_info');
        
    }

    public function change_pwd(){
        // V($_POST);


        // 判断输入框是否为空
        if (empty($_POST)) {
            $this->error('您的输入为空!');
        }
        $pwd = $_POST['pwd'];
        $repwd = $_POST['repwd'];
        if($pwd != $repwd){
            $this->error('两次密码不一致!');
        }

        $user_id = $_SESSION['user']['id'];

        // 验证
     $data= M('user')->where(array('id'=>$user_id))->field('pwd')->select();
     $pwd = $data[0]['pwd'];
     // V($pwd);

        // 验证密码
        if ($pwd != md5($_POST['oldpwd'])) {
            $this->error('密码不正确');
            exit;
        }
        if ($pwd == md5($_POST['pwd'])) {
            $this->error('请重新输入新的密码!');
        }
        $pwd = md5($_POST['pwd']);
        M('user')->where(array('id'=>$user_id))->save(array('pwd'=>$pwd));
        $this -> display('account_info');

    }


    public function account_binding(){
        $this->display("User/account_binding");
    }

    // 编辑用户信息
    public function edit_Ajax()
    {
        // V($_POST);

        // console.log(name);
        // console.log(sex);
        // console.log(company);
        // console.log(job);
        // console.log(address);
        // console.log(email);
        // console.log(phone);
    
       $name = I('post.name');
       $sex = I('post.sex');
       $birthday = I('post.birthday');
       $company = I('post.company');
       $job = I('post.job');
       $address = I('post.address');
       $email = I('post.email');
       $phone = I('post.phone');

       // 取出当前用户ID
       $id = $_SESSION['user']['id'];
        $info = M('user');
        $data = $info ->where(array('id'=>$id))->save(array('real_name'=>$name,'sex'=>$sex,'birthday'=>$birthday,'company'=>$company,'job'=>$job,'address'=>$address,'email'=>$email,'phone'=>$phone));
        if($data) {
            $_SESSION['user']['real_name'] = $name;
            $_SESSION['user']['sex'] = $sex;
            $_SESSION['user']['birthday'] = $birthday;
            $_SESSION['user']['company'] = $company;
            $_SESSION['user']['job'] = $job;
            $_SESSION['user']['address'] = $address;
            $_SESSION['user']['email'] = $email;
            $_SESSION['user']['phone'] = $phone;
            
            $this->ajaxReturn(true);
        } else {
            $this->ajaxReturn(false);
        }

    }
}



